//
//  LNHGoodsDetailBottomView.m
//  LNH
//
//  Created by cruise on 2018/2/7.
//  Copyright © 2018年 twentylx. All rights reserved.
//

#import "LNHGoodsDetailBottomView.h"

#import "UIButton+Extension.h"

#import "LNHGoodsDetailViewModel.h"

@interface LNHGoodsDetailBottomView()

@property (nonatomic, strong) UIButton *homeBtn;
@property (nonatomic, strong) UIButton *shopcarBtn;
@property (nonatomic, strong) UIButton *addShopcarBtn;
@property (nonatomic, strong) UIButton *buyBtn;

@property (nonatomic, strong) LNHGoodsDetailViewModel *viewModel;

@end

@implementation LNHGoodsDetailBottomView

/**
 为视图绑定 viewModel
 
 @param viewModel 要绑定的ViewModel
 @param params 额外参数
 */
- (void)bindViewModel:(id <LMViewProtocol>)viewModel withParams:(NSDictionary *)params {
    if ([viewModel isKindOfClass:[LNHGoodsDetailViewModel class]]){
        self.viewModel = (LNHGoodsDetailViewModel *)viewModel;
    }
    
    
}

/**
 初始化额外数据
 */
- (void)lm_initializeForView {
    
    
}

/**
 初始化视图
 */
- (void)lm_createViewForView {
    
    [self setBackgroundColor:[UIColor whiteColor]];
    
    [self addSubview:self.homeBtn];
    [self addSubview:self.shopcarBtn];
    [self addSubview:self.addShopcarBtn];
    [self addSubview:self.buyBtn];
    
    
    [self setNeedsUpdateConstraints];
}

-(void) updateConstraints {
    
    [self.homeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.left.equalTo(self).offset(5);
        make.bottom.equalTo(self).offset(isIPhoneX ? -34 : 0);
        make.width.mas_equalTo(60);
    }];
    
    [self.shopcarBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.homeBtn);
        make.left.equalTo(self.homeBtn.mas_right);
        make.bottom.equalTo(self.homeBtn);
        make.width.mas_equalTo(60);
    }];
    
    if (self.viewModel.isUpgrade) {
        [self.buyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.homeBtn);
            make.left.equalTo(self.shopcarBtn.mas_right).offset(5);
            make.right.equalTo(self);
            make.bottom.equalTo(self.homeBtn);
        }];
    } else {
        [self.addShopcarBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.homeBtn);
            make.left.equalTo(self.shopcarBtn.mas_right).offset(5);
            make.right.equalTo(self.buyBtn.mas_left);
            make.bottom.equalTo(self.homeBtn);
            make.width.equalTo(self.buyBtn);
        }];
        
        [self.buyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.homeBtn);
            make.left.equalTo(self.addShopcarBtn.mas_right);
            make.right.equalTo(self.mas_right);
            make.bottom.equalTo(self.homeBtn);
            make.width.equalTo(self.addShopcarBtn);
        }];
    }
    
    [super updateConstraints];
    
}

#pragma mark - Getter

-(UIButton *) homeBtn {
    if (!_homeBtn) {
        _homeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_homeBtn setImage:LMImageNamed(@"goods_detail_home_btn") forState:UIControlStateNormal];
        [_homeBtn setTitle:@"首页" forState:UIControlStateNormal];
        [_homeBtn setTitleColor:RGBCOLOR(179, 179, 179) forState:UIControlStateNormal];
        [_homeBtn.titleLabel setFont:[UIFont systemFontOfSize:12]];
        [_homeBtn layoutButtonWithEdgeInsetsStyle:LMButtonEdgeInsetsStyleTop imageTitleSpace:5];
        @weakify(self);
        [[_homeBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            if (self.delegate && [self.delegate respondsToSelector:@selector(goHomeAction)]) {
                [self.delegate goHomeAction];
            }
        }];
    }
    return _homeBtn;
}

-(UIButton *) shopcarBtn {
    if (!_shopcarBtn) {
        _shopcarBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_shopcarBtn setImage:LMImageNamed(@"goods_detail_shopcar_btn") forState:UIControlStateNormal];
        [_shopcarBtn setTitle:@"购物车" forState:UIControlStateNormal];
        [_shopcarBtn setTitleColor:RGBCOLOR(179, 179, 179) forState:UIControlStateNormal];
        [_shopcarBtn.titleLabel setFont:[UIFont systemFontOfSize:12]];
        [_shopcarBtn layoutButtonWithEdgeInsetsStyle:LMButtonEdgeInsetsStyleTop imageTitleSpace:5];
        @weakify(self);
        [[_shopcarBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            if (self.delegate && [self.delegate respondsToSelector:@selector(goToShopcarAction)]) {
                [self.delegate goToShopcarAction];
            }
        }];
    }
    return _shopcarBtn;
}

-(UIButton *) addShopcarBtn {
    if (!_addShopcarBtn) {
        _addShopcarBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_addShopcarBtn setTitle:@"加入购物车" forState:UIControlStateNormal];
        [_addShopcarBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_addShopcarBtn setBackgroundColor:RGBCOLOR(245, 171, 51)];
        [_addShopcarBtn.titleLabel setFont:[UIFont systemFontOfSize:13]];
        @weakify(self);
        [[_addShopcarBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            if (self.delegate && [self.delegate respondsToSelector:@selector(addShopcarAction)]) {
                [self.delegate addShopcarAction];
            }
        }];
    }
    return _addShopcarBtn;
}

-(UIButton *) buyBtn {
    if (!_buyBtn) {
        _buyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_buyBtn setTitle:@"立即购买" forState:UIControlStateNormal];
        [_buyBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_buyBtn setBackgroundColor:RGBCOLOR(234, 119, 66)];
        [_buyBtn.titleLabel setFont:[UIFont systemFontOfSize:13]];
        @weakify(self);
        [[_buyBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            if (self.delegate && [self.delegate respondsToSelector:@selector(buyAction)]) {
                [self.delegate buyAction];
            }
        }];
    }
    return _buyBtn;
}

@end
